package com.shoes.service.impl;

import com.shoes.dao.GoodsDAO;
import com.shoes.model.GoodsDO;
import com.shoes.model.ResponseDO;
import com.shoes.service.GoodsService;
import com.shoes.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author asen
 */
@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {

    private GoodsDAO goodsDAO;

    @Override
    public ResponseDO getGoodsInfoByGoodsId(long userId, long goodsId) throws Exception {
        ResponseDO result;
        GoodsDO goods = goodsDAO.queryGoodsInfoByGoodsId(goodsId);
        if(goods !=null) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, goods);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_RESULT_EMPTY, null);
        }
        return result;
    }

    @Override
    public ResponseDO getSellingGoodsList(long storeId) throws Exception {
        ResponseDO result;
        List<GoodsDO> list = goodsDAO.queryGoodsListByStoreId(storeId, 0);
        if(list != null) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, list);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_RESULT_EMPTY, null);
        }
        return result;
    }

    @Override
    public ResponseDO getRepertoryGoodsList(long storeId) throws Exception {
        ResponseDO result;
        List<GoodsDO> list = goodsDAO.queryGoodsListByStoreId(storeId, 1);
        if(list != null) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, list);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_RESULT_EMPTY, null);
        }
        return result;
    }

    @Override
    public ResponseDO addGoods(long storeId, String name, double price, String categoryIds, int isLike) throws Exception {
        ResponseDO result;
        GoodsDO goods = new GoodsDO();
        goods.setStoreId(storeId);
        goods.setGoodsName(name);
        goods.setPrice(BigDecimal.valueOf(price));
        goods.setgCategoryIds(categoryIds);
        goods.setIsLike(isLike);

        int count = goodsDAO.insertGoods(goods);
        if(count == 1) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, goods.getGoodsId(), null);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null, "inset goods error");
        }
        return result;
    }

    public GoodsDAO getGoodsDAO() {
        return goodsDAO;
    }
    @Autowired
    public void setGoodsDAO(GoodsDAO goodsDAO) {
        this.goodsDAO = goodsDAO;
    }
}
